// pages/travel/travel.js
let QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
let qqmapsdk;
Page({

      /**
       * 页面的初始数据
       */
      data: {
            province:'',// 省份
            city:'',// 城市
            cityList:[],
            toCityId:'',// 到达城市
            formCityId:'',// 出发城市
            toCity:{
                  status:false,
                  province:'',
                  city:'' 
            },
            to_info:{},
            from_info:{},
            showInfo:1,
            health_code:{
                  health_code_desc:'',// 支付宝还是微信
                  health_code_name:'',// 健康码名称
                  health_code_gid:'',// 使用教程
                  health_code_picture:'',// 二维码图片
                  health_code_style:''// 健康码类型
            }
      },
      exit(){
            this.setData({
                  showInfo:1
            })
      },
      showCode(e){
            let health_code={}
            if(e.currentTarget.dataset.name=='from'){
                  health_code.health_code_desc=this.data.from_info.health_code_desc,
                  health_code.health_code_name=this.data.from_info.health_code_name,
                  health_code.health_code_gid=this.data.from_info.health_code_gid,
                  health_code.health_code_picture=this.data.from_info.health_code_picture,
                  health_code.health_code_style=this.data.from_info.health_code_style
            }else{
                  health_code.health_code_desc=this.data.to_info.health_code_desc,
                  health_code.health_code_name=this.data.to_info.health_code_name,
                  health_code.health_code_gid=this.data.to_info.health_code_gid,
                  health_code.health_code_picture=this.data.to_info.health_code_picture,
                  health_code.health_code_style=this.data.to_info.health_code_style
            }
            this.setData({
                  health_code:health_code,
                  showInfo:2
            })

      },
      /**选择目的地城市 */
      bindMultiPickerChange(e){
            // 初始化到达城市信息
            let toCity={
                  status:true,
                  province:e.detail.value[0],
                  city:e.detail.value[1],
            }

            // 查询城市ID
            let city=e.detail.value[1];
            city=city.slice(0,2);
            let province=e.detail.value[0];
            province=province.slice(0,2);
            this.getCityId(province,city);
            this.travelInfo();

            // 设置到达城市信息
            this.setData({
                  toCity:toCity
            })

      },
      //通过省份和城市名称获取 到达 城市ID
      getCityId(province,city){
            this.data.cityList.forEach(item=>{
                  if(item.province.slice(0,2)==province){
                        item.cities.forEach(item2=>{
                              if(item2.city.slice(0,2)==city){
                                    this.setData({
                                          toCityId:item2.city_id
                                    },()=>{
                                          return;
                                    })
                              }
                        })
                  }
            })
            
      },
      /**查询目的地防疫政策 */
      travelInfo(){
            wx.request({
                  url: 'https://v2.alapi.cn/api/springTravel/query?token='+getApp().globalData.alapiToken+'&from='+this.data.formCityId+'&to='+this.data.toCityId,
                  method:'GET',
                  success:res=>{
                        this.setData({
                              to_info:res.data.data.to_info,
                              from_info:res.data.data.from_info
                        })
                  }
            })
      },

      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function (options) {
            qqmapsdk = new QQMapWX({
                  key: 'XELBZ-N6DRX-X7E4F-74U36-YUIAK-SAFDH'
            });
            wx.getLocation({
                  highAccuracyExpireTime:5000,
                  success:res=>{
                        wx.showLoading({
                              title: '初始化数据',
                        })
                        var latitude = res.latitude // 纬度，浮点数，范围为90 ~ -90
                        var longitude = res.longitude // 经度，浮点数，范围为180 ~ -180
                        // 根据经纬度获取所在城市
                        qqmapsdk.reverseGeocoder({
                              location: {latitude: latitude,longitude: longitude},
                              success:addr=>{
                                    this.setData({ 
                                          city: addr.result.address_component.city,
                                          province:addr.result.address_component.province
                                    })
                                    this.initCityList();
                              },fail:()=>{
                                    wx.showToast({
                                          title: '获取位置失败',
                                          icon:'error'
                                    })
                              }
                        });
                  },fail:()=>{
                        wx.showToast({
                              title: '获取位置失败,请打开定位服务',
                              icon:'none',
                              duration:2000
                        })
                  }
            })
            
      },
      /**初始化城市列表 */
      initCityList(){
            wx.request({
                  url: 'https://v2.alapi.cn/api/springTravel/city?token='+getApp().globalData.alapiToken,
                  method:'GET',
                  success:res=>{
                        this.setData({
                              cityList:res.data.data
                        },()=>{
                              wx.hideLoading()
                              this.initFormCityId();
                        })

                  },fail:()=>{
                        wx.showToast({
                              title: '服务器繁忙',
                              icon:'error'
                        })
                  }     
            })
      },
      initFormCityId(){
            // 查询城市ID
            let city=this.data.city;
            city=city.slice(0,city.length-1);
            let province=this.data.province;
            province=province.slice(0,province.length-1);

            // 获取城市ID
            this.data.cityList.forEach(item=>{
                  if(item.province==province){
                        item.cities.forEach(item2=>{
                              if(item2.city==city){
                                    this.setData({
                                          formCityId:item2.city_id
                                          
                                    },()=>{
                                          return;
                                    })
                              }
                        })
                  }
            })
      },

      /**
       * 生命周期函数--监听页面初次渲染完成
       */
      onReady: function () {

      },

      /**
       * 生命周期函数--监听页面显示
       */
      onShow: function () {
            
      },
})